/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smarttmt.idu.web.beans;

import com.smarttmt.idu.web.behavior.Preloadable;
import com.smarttmt.idu.web.model.Chip;
import com.smarttmt.idu.web.model.UserLogin;
import com.smarttmt.idu.web.persistence.controller.UsuacontJpaController;
import com.smarttmt.idu.web.persistence.controller.VwEstadocuentaValorizaJpaController;
import com.smarttmt.idu.web.persistence.entity.Uscochip;
import com.smarttmt.idu.web.persistence.entity.Usuacont;
import com.smarttmt.idu.web.utilities.FacesUtils;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.ConfigurableNavigationHandler;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

/**
 *
 * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
 */
@Named(value = "inquireChipsRegistredBean")
@SessionScoped
public class InquireChipsRegistred extends SmartIduBackingBean implements Serializable, Preloadable {

    @PersistenceUnit(unitName = "smarttmtiduappPU")
    private EntityManagerFactory emf;
    @Resource
    private UserTransaction utx;
    private UsuacontJpaController usuacontJpaController;
    private VwEstadocuentaValorizaJpaController vwEstadocuentaValorizaJpaController;
    private java.util.List<Uscochip> uscoChipList;

    /**
     * Creates a new instance of inquireChipsRegistred
     */
    public InquireChipsRegistred() {
    }

    @PostConstruct
    public void postConstructorInquireChipsRegistred() {
        usuacontJpaController = new UsuacontJpaController(utx, emf);
        vwEstadocuentaValorizaJpaController = new VwEstadocuentaValorizaJpaController(utx, emf);
    }

    public void resetData() {

    }

    @Override
    public void preload(Map<String, Object> parameters) throws Exception {
        resetData();
        UserLogin userLoginBean = (UserLogin) FacesUtils.getManagedBean("userLoginBean");
        Usuacont usuacont = usuacontJpaController.getUserByUsconomb(userLoginBean.getUsername());
        uscoChipList = usuacont.getUscochipList();
    }

    public String getEstadoCuenta(String chipNumber, String inmobRegistryNumber, String inmobAddressNumber) throws Exception {
        String aux = vwEstadocuentaValorizaJpaController.MatriculaDireccion(chipNumber);
        String[] auxSplit = aux.split("\\|");
        inmobRegistryNumber = auxSplit[0];
        inmobAddressNumber = auxSplit[1];

        Chip chip = new Chip(chipNumber, inmobAddressNumber, inmobRegistryNumber);
        java.util.Map<String, Object> parameters = new java.util.HashMap<>();
        parameters.put("chip", chip);
        Preloadable bean = (Preloadable) FacesUtils.getManagedBean("customerOperationsBean");
        bean.preload(parameters);
        return "customerOperations";
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public UserTransaction getUtx() {
        return utx;
    }

    public void setUtx(UserTransaction utx) {
        this.utx = utx;
    }

    public UsuacontJpaController getUsuacontJpaController() {
        return usuacontJpaController;
    }

    public void setUsuacontJpaController(UsuacontJpaController usuacontJpaController) {
        this.usuacontJpaController = usuacontJpaController;
    }

    public List<Uscochip> getUscoChipList() {
        return uscoChipList;
    }

    public void setUscoChipList(List<Uscochip> uscoChipList) {
        this.uscoChipList = uscoChipList;
    }

    public VwEstadocuentaValorizaJpaController getVwEstadocuentaValorizaJpaController() {
        return vwEstadocuentaValorizaJpaController;
    }

    public void setVwEstadocuentaValorizaJpaController(VwEstadocuentaValorizaJpaController vwEstadocuentaValorizaJpaController) {
        this.vwEstadocuentaValorizaJpaController = vwEstadocuentaValorizaJpaController;
    }

}
